package com.ui.basic.security.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ui.basic.ibatis.dao.system.TblSMenuDAO;
import com.ui.basic.ibatis.dao.system.TblSRoleDAO;
import com.ui.basic.ibatis.dao.system.TblSRoleMenusDAO;
import com.ui.basic.ibatis.dao.system.TblSUserDAO;
import com.ui.basic.ibatis.model.system.TblSMenu;
import com.ui.basic.ibatis.model.system.TblSRole;
import com.ui.basic.ibatis.model.system.TblSRoleMenusExample;
import com.ui.basic.ibatis.model.system.TblSRoleMenusKey;
import com.ui.basic.ibatis.model.system.TblSUser;
import com.ui.basic.ibatis.model.system.TblSUserExample;
import com.ui.basic.security.assembler.UserSecurityAssembler;
import com.ui.basic.security.domain.UserSecurity;

public class JSecurityServiceImpl implements JSecurityService {
	private TblSUserDAO tblSUserDAO;
	private TblSRoleDAO tblSRoleDAO;
	private TblSMenuDAO tblSMenuDAO;
	private TblSRoleMenusDAO tblSRoleMenusDAO;

	public UserSecurity findUserSecurity(String username) {
		TblSUserExample example = new TblSUserExample();
		example.createCriteria().andUsernameEqualTo(username);
		UserSecurity userSecurity = null;

		try {
			userSecurity = UserSecurityAssembler
					.toUserSecurity((TblSUser) tblSUserDAO.selectByExample(
							example).get(0));
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

		return userSecurity;
	}

	public UserSecurity getUserSecurity(Long userId) {
		UserSecurity userSecurity = null;
		try {
			userSecurity = UserSecurityAssembler.toUserSecurity(tblSUserDAO
					.selectByPrimaryKey(userId));
			TblSRole tblSRole = tblSRoleDAO.selectByPrimaryKey(userSecurity.getRoleId());
			userSecurity.setTblSRole(tblSRole);
			
			TblSRoleMenusExample example = new TblSRoleMenusExample();
			example.createCriteria().andRoleIdEqualTo(tblSRole.getId());
			
			List<TblSMenu> menus = new ArrayList<TblSMenu>();
			List<TblSRoleMenusKey> roleMenusList = tblSRoleMenusDAO.selectByExample(example);
			for(TblSRoleMenusKey roleMenusKey:roleMenusList){
				menus.add(tblSMenuDAO.selectByPrimaryKey(roleMenusKey.getMenuId()));
			}
			
			userSecurity.setMenus(menus);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

		return userSecurity;
	}

	public void setLastLogin(Long userId) {
		try {
			TblSUser tblSUser = tblSUserDAO.selectByPrimaryKey(userId);
			tblSUser.setLastlogintimestamp(new Date());
			tblSUserDAO.updateByPrimaryKey(tblSUser);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Autowired
	public void setTblSUserDAO(TblSUserDAO tblSUserDAO) {
		this.tblSUserDAO = tblSUserDAO;
	}

	@Autowired
	public void setTblSRoleDAO(TblSRoleDAO tblSRoleDAO) {
		this.tblSRoleDAO = tblSRoleDAO;
	}

	@Autowired
	public void setTblSMenuDAO(TblSMenuDAO tblSMenuDAO) {
		this.tblSMenuDAO = tblSMenuDAO;
	}

	@Autowired
	public void setTblSRoleMenusDAO(TblSRoleMenusDAO tblSRoleMenusDAO) {
		this.tblSRoleMenusDAO = tblSRoleMenusDAO;
	}
}
